﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ExtraClub.Domain.Entities;
using System.Web.Security;
using ExtraClub.Domain;
using System.Net.Mail;
using LB.Generics;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Web.Forms.Service {

    public class client : BaseWS {

        #region ADD UPDATE DELETE

        protected override void Add(Dictionary<string, object> data) {
            UpdateProfile(data);
        }

        protected override void Update(Dictionary<string, object> data) {
            UpdateProfile(data);
        }

        protected override void Delete(Dictionary<string, object> data) {
            if (!EntityId.HasValue) {
                throw new Exception("Not found entity");
            }
            Entity.Delete(EntityId);
        }

        #endregion

        #region Routines
        protected void UpdateProfile(Dictionary<string, object> data) {
            Client client = null;
            Entity entity = null;
            AddressRepository address = null;
            int ClubId = int.Parse( data["clubid"].ToString());

            if (EntityId > 0) {
                client = Client.FetchByID(EntityId);
                entity = Entity.FetchByID(EntityId);
                address = AddressRepository.FetchByID(EntityId);
            } else {
                entity = new Entity();
                client = new Client();
                address = new AddressRepository();
            }

            client.FirstName = data[Client.Columns.FirstName.ToLower()].ToString();
            client.FamilyName = data[Client.Columns.FamilyName.ToLower()].ToString();
            client.IDNumber = StringExt.IsLong(data[Client.Columns.IDNumber.ToLower()]);
            client.Sex = data[Client.Columns.Sex.ToLower()].ToString();

            client.BirthDate = DateTime.Parse(data[Client.Columns.BirthDate.ToLower()].ToString(), null, System.Globalization.DateTimeStyles.AdjustToUniversal);

            client.MarrageState = data[Client.Columns.MarrageState.ToLower()].ToString();
            client.NumberOfChildren = StringExt.IsByte(data[Client.Columns.NumberOfChildren.ToLower()]);
            client.Education = data[Client.Columns.Education.ToLower()].ToString();
            client.MothersFamilyName = data[Client.Columns.MothersFamilyName.ToLower()].ToString();

            SetAddress(ref address, data);

            if (EntityId > 0) {
                entity.Modified = DateTime.Now;
                entity.Save((Guid)SiteContext.Current.User.ProviderUserKey);
            } else {
                entity.EntityType = "Client";
                entity.Created = DateTime.Now;
                entity.ParentId = ClubId;
                entity.Save((Guid)SiteContext.Current.User.ProviderUserKey);

                client.Id = entity.Id;
                address.EntityId = entity.Id;
            }
            client.Save();
            address.Save();
        }
        #endregion
    }
}
